<?php


namespace app\controller\user;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 用户管理
 */
class Index extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 登录
     */
	public function api_user_index_login($msg)
    {   
        global $db;
        global $db1;
        global $redis;
        $username = isset($msg['data']['username'])?$msg['data']['username']:'';
        $password = isset($msg['data']['password'])?$msg['data']['password']:'';    
        $login_type = isset($data['login_type'])?$data['login_type']:1;
        $token = $this->common->createToken();
        if(empty($username) || empty($password)){
            return $this->outMessage(-1,'用户名或密码不能为空');
        }
        if($login_type < 1 && $login_type > 3){
            return $this->outMessage(-1,'登录类型不正确');
        }
        if($login_type == 1){
            $user = $db1->select('*')->from('admin')->where(['username'=>$username])->row();
            if(empty($user)){
                return $this->outMessage(-1,'该账号不存在');
            }
            if($user['password'] != md5(md5($password) . $user['salt'])) {
                return $this->outMessage(-1,'密码错误');
            }
            $admin = $this->table->find('admin',['union_id' => $user['union_id']]);
            if($admin['status'] == 2){
                return $this->outMessage(-1,'该账户已被锁定,请联系管理员.');
            }
            $redis->set($token,json_encode($admin));
            $_SESSION['admin_info'] = ['admin_id'=>$admin['id'],'union_id'=>$admin['union_id']];
            $admin['token'] = $token;
            $admin['login_type'] = $login_type;
            return $this->outMessage(10000,'登录成功',$admin,'success',count($admin));
        }else{
            return $this->outMessage(-1,'暂未开发,请等待.');
        }
        
	}




 

}
